<?php
/**
 * @package		简易CMS
 * @subpackage	插件函数包
 * @version		$Id: function_ranklist.php 19 2012-06-07 17:36:32Z htmambo@gmail.com $
 * @author		Hoping
 * @copyright	Copyright (C) 2010 Hoping Software Studio.
 */

if(!defined('IN_JYCMS')) {
	exit('Access Denied');
}

function getranklist_article($num = 20, $dateline = 0, $orderby = 'dateline DESC') {
	$dateline = !$dateline ? TIMESTAMP - 86400 * 30 : $dateline;
	$query = DB::query("SELECT a.aid, a.uid, a.username, a.title, a.summary as message, a.dateline,
	a.viewnum, a.commentnum
		FROM #__portal_article_title a
		WHERE a.dateline>='$dateline'
		ORDER BY $orderby
		LIMIT 0, $num");
	require_once libfile('function/misc');
	$rank = 0;
	while($arc = DB::fetch($query)) {
		++$rank;
		$arc['rank'] = $rank;
		$arc['dateline'] = dgmdate($arc['dateline']);
		$arc['message'] = preg_replace('/<([^>]*?)>/', '', $arc['message']);
		$arcs[] = $arc;
	}
	return $arcs;
}


function getranklistcache_article() {
	global $_G, $cache_time, $cache_num, $dateline, $orderby;
	$ranklistvars = array();
	loadcache('ranklist_article');
	$ranklistvars = & $_G['cache']['ranklist_article'][$_G['gp_before'].'_'.$_G['gp_orderby']];

	if(!empty($ranklistvars['lastupdated']) && TIMESTAMP - $ranklistvars['lastupdated'] < $cache_time) {
		return $ranklistvars;
	}

	$ranklistvars = getranklist_article($cache_num, $dateline, $orderby);

	$ranklistvars['lastupdated'] = TIMESTAMP;
	$ranklistvars['lastupdate'] = dgmdate(TIMESTAMP);
	$ranklistvars['nextupdate'] = dgmdate(TIMESTAMP + $cache_time);
	$_G['cache']['ranklist_article'][$_G['gp_before'].'_'.$_G['gp_orderby']] = $ranklistvars;
	require_once libfile('function/cache');
	save_syscache('ranklist_article', $_G['cache']['ranklist_article']);
	return $ranklistvars;
}


function getranklistcache($name, $dateline = '') {
	global $_G;
	if(is_ranklistcache_available($name) || $_G['ranklist_cacheupdated']) {
		$ranklist = $_G['cache']['ranklist_index'][$name];
		unset($ranklist['lastupdated']);
		return $ranklist;
	}

	switch($name) {
		case 'article_hot':
			//$ranklist = getranklist_article(10, $dateline, 'heats DESC');
			break;
		case 'article_fav':
			//$ranklist = getranklist_article(10, 0, 'favtimes DESC');
			break;
	}
	$ranklist['lastupdated'] = TIMESTAMP;
	ranklist_cache_push($name, $ranklist);
	return $ranklist;
}

function ranklist_cache_push($name, $ranklist) {
	global $_G;
	$_G['cache']['ranklist_index'][$name] = $ranklist;
	$_G['cache']['ranklist_index']['lastupdated'] = $ranklist['lastupdated'];
	$_G['ranklist_cacheupdated'] = true;
}
